package com.hbsx.why.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.hbsx.why.http.HttpAdapter;
import com.hbsx.why.http.HttpSerives;
import com.hbsx.why.utils.CommonUtils;
import com.hbsx.why.utils.LogUtils;
import com.hbsx.why.utils.ToastUtils;
import com.hbsx.why.utils.changeskin.SkinManager;
import com.squareup.leakcanary.RefWatcher;

import butterknife.ButterKnife;

/**
 * Created by cc on 16-7-9.
 */
public abstract class BaseFragment extends Fragment {
    protected View mainView;
    protected boolean isViewInitiated;
    protected boolean misVisibleToUser;
    protected boolean isDataInitiated;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public HttpSerives getServer() {
        return HttpAdapter.getSerives();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //SkinManager.getInstance().register(getActivity());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getActivity().getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        if (getLayout() <= 0) {
            mainView = createMainView();
        } else {
            mainView = inflater.inflate(getLayout(), container, false);
        }
        if (mainView == null)
            throw new NullPointerException("hey,you forget set a view for " + getClass().getName());
        ButterKnife.bind(this, mainView);
        return mainView;
    }

//    private void setFitSystem() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            int statusBarHeight = CommonUtils.getStatusHeight(getActivity());
//            View view = getView();
//            view.setPadding(0, statusBarHeight, 0, 0);
//        }
//    }

    private int getFitSystemHeight() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int statusBarHeight = CommonUtils.getStatusHeight(getActivity());
            return statusBarHeight;
        } else {
            return 0;
        }
    }

    /**
     * 设置控件适应沉浸式
     *
     * @param view
     */
    public void setFitSystem(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int statusBarHeight = CommonUtils.getStatusHeight(getActivity());
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            layoutParams.setMargins(layoutParams.leftMargin, statusBarHeight, layoutParams.rightMargin, layoutParams.bottomMargin);
        }
    }

    /**
     * 是否适应沉浸式
     *
     * @return
     */
    public boolean getFitSystem() {
        return false;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onInit();
        if (onInitCompleteListener != null)
            onInitCompleteListener.onInitComplete();
//        if (getFitSystem()) {
//            setFitSystem();
//        }
    }

    protected View findViewById(@IdRes int id) {
        return mainView.findViewById(id);
    }

    @LayoutRes
    protected abstract int getLayout();

    protected View createMainView() {
        return null;
    }

    /**
     * 数据初始化
     */
    protected abstract void onInit();

    protected void launch(Class clazz) {
        startActivity(new Intent(getActivity(), clazz));
    }

    protected void launch(Class clazz, Intent intent) {
        intent.setClass(getActivity(), clazz);
        startActivity(intent);
    }

    protected void showToast(String msg) {
        ToastUtils.toastShort(msg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = BaseApplication.getRefWatcher(getActivity());
        refWatcher.watch(this);
        //EventBus.getDefault().unregister(this);
    }

    OnInitCompleteListener onInitCompleteListener;

    public void setOnInitCompleteListener(OnInitCompleteListener onInitCompleteListener) {
        this.onInitCompleteListener = onInitCompleteListener;
    }

    public interface OnInitCompleteListener {
        void onInitComplete();
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtils.logi("------setUserVisibleHint当前加载的类名=" + getClass().getName());
//        LogUtils.logi("-----开始界面isVisibleToUser=" + isVisibleToUser);
//        LogUtils.logi("-----开始界面getUserVisibleHint()=" + getUserVisibleHint());
        if (getUserVisibleHint()) {
            this.misVisibleToUser = true;
            // LogUtils.logi("-----开始界面="+isVisibleToUser);
        } else {
            if (misVisibleToUser) {
                this.misVisibleToUser = false;
            }
        }
        //LogUtils.logi("-----isVisibleToUser的值="+isVisibleToUser);
        prepareFetchData(isVisibleToUser);
    }

    public abstract void fetchData();

    public boolean prepareFetchData() {
        return prepareFetchData(false);
    }

    public boolean prepareFetchData(boolean forceUpdate) {
        if (misVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            fetchData();
            // LogUtils.logi("----- getClass().getName()=" + forceUpdate);
            isDataInitiated = true;
            return true;
        }
        return false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // SkinManager.getInstance().unregister(getActivity());
    }
}
